/** Title screen mode handler.
 */
function TitleScreenMode()
{
	this.buffer = new ScreenBuffer(host.screen.width, host.screen.height);
	this.generate();
	this.render();
}
TitleScreenMode.prototype =
{
	/** Generate the desert horizon background.
	 */
	generate: function()
	{
		var self = this;

		/** Cloud blob method, creates a noisy blob of alpha-blended white.
		 *@param x Center X.
		 *@param y Center Y.
		 */
		var cloudOfs = [0, 0, 0, -1, 0, 1, -1, 0, 1, 0];
		function cloudBlob(x, y)
		{
			var i, ix, iy, a, cell, c;
			for(i = 0; i < cloudOfs.length; i += 2)
			{
				ix = x + cloudOfs[i];
				iy = y + cloudOfs[i + 1];
				a = Math.random() * 0.25;
				a += 0.25;
				cell = self.buffer.getCell(ix, iy);
				if(cell == null)
					continue;
				c = cell.bg.alphaBlend(Color.white, a);
				self.buffer.setCell(ix, iy, null, null, c);
			}
		}

		// Generate the ground and sky
		var groundTopY = Math.floor(this.buffer.height / 2.25);
		var skyTop = new Color(0.16, 0.235, 0.51);
		var skyBottom = new Color(0.51, 0.835, 1);
		var groundTop = new Color(1, 0.85, 0);
		var groundBottom = new Color(0.66, 0.33, 0);
		var i = 0, j, c;
		while(i < groundTopY)
		{
			for(j = 0; j < this.buffer.width; ++j)
			{
				c = Color.interpolate(skyTop, skyBottom, i / (groundTopY - 1));
				c.r += Math.random() * 0.025 - 0.0125;
				c.g += Math.random() * 0.025 - 0.0125;
				c.b += Math.random() * 0.025 - 0.0125;
				this.buffer.setCell(j, i, " ", null, c);
			}
			++i;
		}
		while(i < this.buffer.height)
		{
			for(j = 0; j < this.buffer.width; ++j)
			{
				c = Color.interpolate(groundTop, groundBottom, (i - groundTopY) / (this.buffer.height - groundTopY));
				c.r += Math.random() * 0.05 - 0.025;
				c.g += Math.random() * 0.05 - 0.025;
				c.b += Math.random() * 0.05 - 0.025;
				this.buffer.setCell(j, i, " ", null, c);
			}
			++i;
		}

		// Generate clouds
		var cx, cy, height, length, count = Math.floor(Math.random() * 5) + 5;
		for(i = 0; i < count; ++i)
		{
			cx = Math.floor(Math.random() * this.buffer.width);
			cy = Math.floor(Math.random() * groundTopY) - 1;
			length = Math.floor(Math.random() * this.buffer.width / 3) + 4;
			for(j = 0; j < length; ++j)
			{
				cloudBlob(cx + j, cy);
			}
		}

		// Generate cacti
		var cactiTop = new Color(0.15, 1, 0.05);
		var cactiBottom = new Color(0, 0.75, 0.15);
		for(i = 0; i < 25; ++i)
		{
			if(i > 15)
			{
				cy = Math.floor(Math.random() * (this.buffer.height - groundTopY));
				height = Math.floor(cy / 4) + 3;
				cy += groundTopY - 1;
			}
			else
			{
				cy = Math.floor(Math.random() * (this.buffer.height - groundTopY * 1.75));
				height = Math.floor(cy / 4) + 3;
				cy += groundTopY - 1;
			}
			cx = Math.floor(Math.random() * this.buffer.width);
			for(j = 0; j < height; ++j)
			{
				this.buffer.setCell(cx, cy + j, ";", Color.white, Color.interpolate(cactiTop, cactiBottom, j / (height - 1)));
			}
		}
	},
	/** Create the main menu.
	 */
	createMainMenu: function()
	{
		var menuOptions = [];
		if(isSaveAvailable())
			menuOptions.push("Continue Game");
		menuOptions.push("New Game", "About");
		modeStack.push(
			new MenuListMode(
				host.screen,
				20,
				null,
				menuOptions,
				this.menuCallback,
				this,
				true,
				true
			)
		);
	},
	/** Menu callback.
	 */
	menuCallback: function(action, source)
	{
		switch(action)
		{
			case "Continue Game":
				if(loadGame())
				{
					source.close();
					removeMode(this);
				}
				break;
			case "New Game":
				if(newGame())
				{
					source.close();
					removeMode(this);
				}
				break;
			case "About":
				textBox("About");
				break;
		}
		return true;
	},
	/** Render the title image to the screen.
	 */
	render: function()
	{
		this.buffer.blitTo(host.screen, 0, 0, true);

		// Title, version and author overlay
		var lines = ["Shambletown Ver. " + version, "by", "Norman B. Lancaster (QBRADQ)"];
		var line, i, tx, ty = host.screen.height - (4 + lines.length);
		for(i = 0; i < lines.length; ++i)
		{
			line = lines[i];
			tx = Math.floor((host.screen.width - line.length) / 2);
			host.screen.blitString(tx, ty, "%4g" + line);
			++ty;
		}
	}
};
